const cloud = require('wx-server-sdk');

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
});

/**
 * 新增活动
 */
exports.main = async (event, context) => {
    const {userInfo, name, creatorId, seasonId} = event;

    if (!userInfo) {
        return {};
    }

    const db = cloud.database();
    const _ = db.command;

    // 新增活动
    return await db.collection('activity').add({
        data: {
            name,
            creatorId,
            seasonId,
            joiners: [],
            time: Date.now()
        }
    }).then(({_id}) => {
        db.collection('season').doc(seasonId).update({
            data: {
                activityIds: _.push(_id)
            }
        })
    });
}
